package com.wanhe.base.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.wanhe.base.BaseApplication;

import java.util.Set;

public class SPUtils {

	public static final String PREFERENCE_NAME = "wanhe_app";
	/**
	 * 记录用户的输入的ip地址和端口号码
	 */
	public static final String IP = "ip";
	public static final String PORT = "port";
	public static  final  String not_validate_pwd = "not_validate_pwd";
	public static  final  String demonstration = "demonstration";
	public static final  String key_account = "account";
	public static final  String key_password = "password";
	private static final String CHECK_ORDER = "check_order";
	public static final String KEY_ALLDISH = "getAllDish";
	public static final String KEY_DESIGN = "getDesignData";
	public static final String KEY_SALL_OUT = "getSallOut";

	public static final String KEY_BID="webSocket_bid";
	public static final String KEY_SOURCE="webSocket_source";

	public static final String KEY_VERSION = "version";

	public static void setBid(String bid){
		setEntryPreference(BaseApplication.context,KEY_BID,bid);
	}
	public static  String getKeySource(){
		return getStringPreference(BaseApplication.context,KEY_SOURCE,"");
	}

	public static void setKeySource(String source){
		setEntryPreference(BaseApplication.context,KEY_SOURCE,source);
	}
	public static String getBid(){
		return getStringPreference(BaseApplication.context,KEY_BID,"");
	}
	public static String getUrl(){
		return "http://"+getIp()+":"+getPort();
	}

	public static String getResoureUrl(){
		return "http://"+getIp()+":"+"8023";
	}
	public static  String getIp(){
		return getStringPreference(BaseApplication.context,IP,"");
	}
	public static  void setIp(String Ip){
		setEntryPreference(BaseApplication.context,IP,Ip);
	}
	public static String getPort(){
		return getStringPreference(BaseApplication.context,PORT,"");
	}
	public static void setPort( String port){
		setEntryPreference(BaseApplication.context,PORT,port);
	}

	public static String getVersion() {
		return getStringPreference(BaseApplication.context,KEY_VERSION,"");
	}

	public static void setVersion(String version) {
		setEntryPreference(BaseApplication.context,KEY_VERSION,version);
	}
	public static String getAccount() {
		return getStringPreference(BaseApplication.context,key_account,"");
	}

	public static void setAccount(String account) {
		setEntryPreference(BaseApplication.context,key_account,account);
	}

	public static String getPassword() {
		return getStringPreference(BaseApplication.context,key_password,"");
	}

	public static  void setPassword(String password) {
		setEntryPreference(BaseApplication.context,key_password,password);
	}

	public static boolean getValidatePwd(){
		return getBooleanPreference(BaseApplication.context,not_validate_pwd,false);
	}

	public static void setValidatePwd(boolean isNot){
		setEntryPreference(BaseApplication.context,not_validate_pwd,isNot);
	}

	public static boolean getIsDemoSchema(){
		return getBooleanPreference(BaseApplication.context,demonstration,false);
	}

	public static void setIsSchema(boolean isNot){
		setEntryPreference(BaseApplication.context,demonstration,isNot);
	}

	/**
	 * 是否核单
	 * @return
	 */
	public static boolean isCheckOrder() {
		return getBooleanPreference(BaseApplication.context,CHECK_ORDER,false);
	}

	/**
	 * 设置是否需要核单
	 * @param isCheck
	 */
	public static void setCheckOrder(boolean isCheck) {
		setEntryPreference(BaseApplication.context,CHECK_ORDER,isCheck);
	}

	public static String getDesign(){
		return getStringPreference(BaseApplication.context, KEY_DESIGN, "");
	}

	public static void setDesign(String design){
		setEntryPreference(BaseApplication.context,KEY_DESIGN,design);
	}


	public static String getAllDish(){
		return getStringPreference(BaseApplication.context, KEY_ALLDISH, "");
	}

	public static void setAllDish(String dish){
		setEntryPreference(BaseApplication.context,KEY_ALLDISH,dish);
	}

	public static String getSallOutData(){
		return getStringPreference(BaseApplication.context, KEY_SALL_OUT, "");
	}

	public static void setSallOutData(String data){
		setEntryPreference(BaseApplication.context,KEY_SALL_OUT,data);
	}

	/**
	 *
	 * @param context
	 * @param name
	 * @return
	 */
	public static SharedPreferences getSharedPreferences(Context context, String name) {
		return context.getSharedPreferences(name, Context.MODE_PRIVATE);
	}

	public static String getStringPreference(Context context, String key, String defaultValue) {
		String result = defaultValue;
		try {
			result = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE).getString(key, defaultValue);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static int getIntPreference(Context context, String key, int defValue) {
		int result = defValue;
		try {
			result = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE).getInt(key, defValue);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static long getLongPreference(Context context, String key, long defValue) {
		long result = defValue;
		try {
			result = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE).getLong(key, defValue);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static boolean getBooleanPreference(Context context, String key, boolean defValue) {
		boolean result = defValue;
		try {
			result = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE).getBoolean(key, defValue);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 
	 * @Title: setEntryPreference 
	 * @Description: 只能传 string int long boolean  Float 5中类型
	 * @return boolean 返回类型
	 */
	public static boolean setEntryPreference(Context context, String key, Object value){
		Editor editor = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE).edit();
		if(value instanceof String){
			editor.putString(key, String.valueOf(value));
		}else if(value instanceof Integer){
			editor.putInt(key, (Integer) value);
		}else if(value instanceof Boolean){
			editor.putBoolean(key, (Boolean) value);
		}else if(value instanceof Long){
			editor.putLong(key, (Long) value);
		}else if(value instanceof Float){
			editor.putLong(key, (Long) value);
		}else if(value instanceof Set){
			editor.putStringSet(key, (Set)value);
		}
//
		return editor.commit();
	}


	
	public static boolean remove(Context context, String key) {
		return context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE).edit().remove(key).commit();
	}	
	
	public static Set<String> getHashSet(Context context, String Key){
		Set<String> result = null;
		try {
			result = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE).getStringSet(Key, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
